package com.cskaoyan.service.wxcoupon;

import com.cskaoyan.bean.po.ohter.MarketCart;
import com.cskaoyan.bean.po.ohter.MarketCartExample;
import com.cskaoyan.bean.po.promotionpo.MarketCoupon;
import com.cskaoyan.bean.po.promotionpo.MarketCouponExample;
import com.cskaoyan.bean.po.promotionpo.MarketCouponUser;
import com.cskaoyan.bean.po.promotionpo.MarketCouponUserExample;
import com.cskaoyan.bean.vo.wxcoupon.CouponMyListInfo;
import com.cskaoyan.bean.vo.wxcoupon.CouponMyListVo;
import com.cskaoyan.bean.vo.wxcoupon.CouponSelectListInfo;
import com.cskaoyan.bean.vo.wxcoupon.CouponSelectListVo;
import com.cskaoyan.mapper.MarketCartMapper;
import com.cskaoyan.mapper.promotion.MarketCouponMapper;
import com.cskaoyan.mapper.promotion.MarketCouponUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @auther zk
 * @date 2022/7/21 15:19
 */
@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;
    @Autowired
    MarketCartMapper marketCartMapper;

    @Override
    public CouponMyListVo couponMyList(Integer userId, Short status, Integer limit, Integer page) {

        PageHelper.startPage(page, limit);
        MarketCouponUserExample example = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId).andStatusEqualTo(status);

        List<MarketCouponUser> userCoupons = marketCouponUserMapper.selectByExample(example);

        PageInfo<MarketCouponUser> pageInfo = new PageInfo<>(userCoupons);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        List<CouponMyListInfo> list = new ArrayList<>();
        for (int i = 0; i < userCoupons.size(); i++) {
            if (i >= limit * (page - 1) && i < limit * page) {
                MarketCouponUser userCoupon = userCoupons.get(i);
                CouponMyListInfo info = new CouponMyListInfo();
                MarketCoupon coupon = marketCouponMapper.selectByPrimaryKey(userCoupon.getCouponId());
                info.setId(userCoupon.getId());
                info.setCid(userCoupon.getCouponId());
                info.setName(coupon.getName());
                info.setDesc(coupon.getDesc());
                info.setTag(coupon.getTag());
                info.setMin(coupon.getMin());
                info.setDiscount(coupon.getDiscount());
                info.setStartTime(coupon.getStartTime());
                info.setEndTime(coupon.getEndTime());
                info.setAvailable(coupon.getDeleted());
                list.add(info);
            }
        }

        CouponMyListVo couponMyListVo = new CouponMyListVo();
        couponMyListVo.setTotal((int) total);
        couponMyListVo.setPages(pages);
        couponMyListVo.setLimit(limit);
        couponMyListVo.setPage(page);
        couponMyListVo.setList(list);

        return couponMyListVo;
    }

    @Override
    public Integer couponReceive(Integer couponId, Integer userId) {

        MarketCoupon marketCoupon1 = marketCouponMapper.selectByPrimaryKey(couponId);
        // 判断优惠卷是否领完
        if (marketCoupon1.getTotal() < 1) {
            return 1;
        }
        // 判断是否超过领取数量限制
        MarketCouponUserExample marketCouponUserExample1 = new MarketCouponUserExample();
        marketCouponUserExample1.createCriteria().andCouponIdEqualTo(couponId).andUserIdEqualTo(userId);
        List<MarketCouponUser> marketUserCoupons = marketCouponUserMapper.selectByExample(marketCouponUserExample1);

        if (marketCoupon1.getLimit() != 0 && marketUserCoupons.size() >= marketCoupon1.getLimit()) {
            return -(int) marketCoupon1.getLimit();
        }
        // 向用户-优惠卷表中插入一条数据
        MarketCouponUser marketCouponUser = new MarketCouponUser();
        marketCouponUser.setUserId(userId);
        marketCouponUser.setCouponId(couponId);
        marketCouponUser.setStartTime(marketCoupon1.getStartTime());
        marketCouponUser.setEndTime(marketCoupon1.getEndTime());
        marketCouponUser.setAddTime(new Date(System.currentTimeMillis()));
        marketCouponUser.setUpdateTime(new Date(System.currentTimeMillis()));
        marketCouponUserMapper.insertSelective(marketCouponUser);
        // 若领取成功，优惠卷数量减1
        MarketCoupon marketCoupon2 = new MarketCoupon();
        marketCoupon2.setTotal(marketCoupon1.getTotal() - 1);
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        marketCouponExample.createCriteria().andIdEqualTo(couponId);
        marketCouponMapper.updateByExampleSelective(marketCoupon2, marketCouponExample);
        return 0;
    }

    @Override
    public CouponSelectListVo couponSelectList(Integer userId, Integer cartId, Integer grouponRulesId) {
        System.out.println(userId);
        System.out.println(cartId);
        // 查出订单信息
        MarketCart marketCart = marketCartMapper.selectByPrimaryKey(cartId);
        Integer number = (int) marketCart.getNumber();
        Integer price = marketCart.getPrice().intValue();

        // 查出用户优惠卷列表信息
        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        marketCouponUserExample.createCriteria()
                .andUserIdEqualTo(userId)
                .andDeletedEqualTo(false);
        List<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByExample(marketCouponUserExample);

        List<CouponSelectListInfo> infoList = new ArrayList<>();
        // 根据不同条件设置available值
        for (MarketCouponUser userCoupon : marketCouponUsers) {
            MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(userCoupon.getCouponId());
            CouponSelectListInfo info = new CouponSelectListInfo();
            Integer totalMoney = number * price;
            int min = marketCoupon.getMin().intValue();
            Short status = userCoupon.getStatus();
            Date startTime = userCoupon.getStartTime();
            Date endTime = userCoupon.getEndTime();
            Date dateNow = new Date(System.currentTimeMillis());
            if (totalMoney < min || status != 0) {
                info.setAvailable(false);
            }
            info.setAvailable(true);
            info.setEndTime(endTime);
            info.setStartTime(startTime);
            info.setDiscount(marketCoupon.getDiscount());
            info.setMin(marketCoupon.getMin());
            info.setTag(marketCoupon.getTag());
            info.setDesc(marketCoupon.getDesc());
            info.setCid(marketCoupon.getId());
            info.setId(userCoupon.getId());
            infoList.add(info);
        }
        CouponSelectListVo couponSelectListVo = new CouponSelectListVo();
        couponSelectListVo.setList(infoList);
        couponSelectListVo.setTotal(infoList.size());
        couponSelectListVo.setPages(1);
        couponSelectListVo.setLimit(infoList.size());
        couponSelectListVo.setPage(1);
        return couponSelectListVo;
    }

    @Override
    public Integer couponExchange(Integer userId, String code) {
        // 判断兑换码是否可用 优惠卷是否领完
        MarketCouponExample example1 = new MarketCouponExample();
        example1.createCriteria()
                .andCodeEqualTo(code)
                .andDeletedEqualTo(false);
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(example1);
        if (marketCoupons.size() < 1) {
            return 1;
        }
        MarketCoupon coupon = marketCoupons.get(0);
        if (coupon.getTotal() < 1) {
            return 2;
        }
        // 优惠卷数量减1
        MarketCouponExample example = new MarketCouponExample();
        example.createCriteria().andIdEqualTo(coupon.getId());
        MarketCoupon marketCoupon = new MarketCoupon();
        marketCoupon.setTotal(coupon.getTotal() - 1);
        marketCouponMapper.updateByExampleSelective(marketCoupon, example);
        // 在用户优惠卷列表中插入一条数据
        MarketCouponUser marketCouponUser = new MarketCouponUser();
        marketCouponUser.setUserId(userId);
        marketCouponUser.setCouponId(coupon.getId());
        marketCouponUser.setStartTime(coupon.getStartTime());
        marketCouponUser.setEndTime(coupon.getEndTime());
        marketCouponUser.setAddTime(new Date(System.currentTimeMillis()));
        marketCouponUser.setUpdateTime(new Date(System.currentTimeMillis()));
        marketCouponUserMapper.insertSelective(marketCouponUser);

        return 0;
    }
}
